// TMMDlg.h : header file
//

#pragma once
#include "afxcmn.h"

#define BUFFER_SIZE                  (1024)
#define PACKAGE_DATA_LEN             (BUFFER_SIZE - 2 * sizeof(BYTE))
#define PACKAGE_DATA_COMMAND_LEN     (PACKAGE_DATA_LEN - 2 * sizeof(DWORD))
#define PACKAGE_DATA_COMMAND_TASKLEN (PACKAGE_DATA_COMMAND_LEN - sizeof(DWORD))

#define PT_HANDSHAKE      (1)
#define PT_HANDSHAKE_ACK  (2)
#define PT_RUN            (3)
#define PT_RUN_ACK        (4)
#define PT_KILL           (5)
#define PT_KILL_ACK       (6)
#define PT_FINISH         (7)
#define PT_FINISH_ACK     (8)

static char* szTitleMap[] = { "NONE", 
							"HANDSHAKE", 
							"HANDSHAKE_ACK", 
							"RUN", 
							"RUN_ACK", 
							"KILL", 
							"KILL_ACK", 
							"FINISH", 
							"FINISH_ACK"
};

typedef struct _Package{
	BYTE head;
	BYTE data[PACKAGE_DATA_LEN];
	BYTE tail;
}PACKAGE, *PPACKAGE;

typedef struct _PackageData{
	DWORD dwType;
	DWORD dwLength;
	BYTE  command[PACKAGE_DATA_COMMAND_LEN];
}PACKAGEDATA, *PPACKAGEDATA;

typedef struct _RunTask{
	DWORD dwId;
	char  task[PACKAGE_DATA_COMMAND_TASKLEN]; 
}RUNTASKPACKAGE, *PRUNTASKPACKAGE;

typedef struct _FinishAck{
	DWORD dwId; 
}FINISHACKPACKAGE, *PFINISHACKPACKAGE;

typedef struct _KillTask{
	DWORD dwId; 
}KILLPACKAGE, *PKILLPACKAGE;

typedef struct _Finish{
	DWORD dwId;
}FINISHPACKAGE, *PFINISHPACKAGE;

typedef struct _HandshakeAck{
	DWORD dwServerId;
	DWORD dwCpus;
}HANDSHAKEACKPACKAGE, *PHANDSHAKEACKPACKAGE;

/***************************************************************/

typedef struct _SubThreadData{
	SOCKET socket;
	CListCtrl * pList;
}SUBTHREADDATA, *PSUBTHREADDATA;

// CTMMDlg dialog
class CTMMDlg : public CDialog
{
// Construction
public:
	CTMMDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_TMM_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
protected:
	HICON m_hIcon;
#if 1
	//
	// sub thread function
	static UINT SubTreadFunc(LPVOID pData);
#endif
	//
	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnBnClickedOk();

	DECLARE_MESSAGE_MAP()
public:
	DWORD          m_dwPort;
	CIPAddressCtrl m_ipCtrl;
	CListCtrl      m_lcPackageView;
	CSocket        m_socket;

	SUBTHREADDATA  m_data;

	static CMap<int, int, int, int> m_map;

	afx_msg void OnBnClickedCancel();
	afx_msg void OnTimer(UINT_PTR nIDEvent);
};
